package com.sri.system;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class RentalAuthenticationFilter implements Filter {

  public RentalAuthenticationFilter() {
    // TODO Auto-generated constructor stub
  }

  public void destroy() {
    // TODO Auto-generated method stub

  }

  public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
    long strTime = 0;
    HttpServletRequest hreq = (HttpServletRequest) req;
    HttpServletResponse hresp = (HttpServletResponse) resp;
    try {
      HttpSession session = hreq.getSession(false);
      if (session.getAttribute("rUser") == null) {
        hresp.sendRedirect("/userLogin.jsp");
      }// if
      else {
        strTime = System.currentTimeMillis();
        chain.doFilter(req, resp);
        System.out.println("Statistics for : \n request : - " + hreq.getQueryString());
        System.out.print(" Time : - " + (System.currentTimeMillis() - strTime));
      }
    }// try
    catch (Exception e) {
    }
  }// doFilter()

  public void init(FilterConfig arg0) throws ServletException {
    // TODO Auto-generated method stub

  }

}
